package la.bbs558.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import la.bbs558.dao.UserSuperviseDao;
import la.bbs558.entity.BbsPost;
import la.bbs558.entity.BbsReply;
import la.bbs558.entity.User;
import la.bbs558.entity.UserSupervise;
import la.bbs558.exception.MsgException;
import la.bbs558.service.BbsPostService;
import la.bbs558.service.BbsReplyService;
import la.bbs558.service.UserService;
import la.bbs558.service.UserSuperviseService;
import la.bbs558.util.Objs;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 用户监管(UserSupervise)表服务实现类
 *
 * @author makejava
 * @since 2024-01-23 10:30:58
 */
@Service("userSuperviseService")
public class UserSuperviseServiceImpl extends ServiceImpl<UserSuperviseDao, UserSupervise> implements UserSuperviseService {
    @Resource
    private BbsPostService bbsPostService;
    @Resource
    private BbsReplyService bbsReplyService;
    @Resource
    private UserService userService;

    @Override
    public boolean supervise(UserSupervise userSupervise) {
        userSupervise.setFromUserId(StpUtil.getLoginIdAsLong());
        Long userId = userSupervise.getUserId();
        Long fromUserId = userSupervise.getFromUserId();
        Objs.isTrue(!Objs.equal(fromUserId, userId),"不能屏蔽或举报自己");
        if(userSupervise.getId() != null){
            userSupervise.setId(null);
        }
        Long postId = userSupervise.getPostId();
        Long replyId = userSupervise.getReplyId();
        if(Objs.isAllInvalid(userId, postId, replyId)){
            throw new MsgException("缺少必要参数");
        }
        if(userId != null){
            User user = userService.getById(userId);
            Objs.isTrue(user != null,"用户不存在");
        }
        if(postId != null){
            BbsPost bbsPost = bbsPostService.getById(postId);
            Objs.isTrue(bbsPost != null,"帖子不存在");
        }
        if(replyId != null){
            BbsReply bbsReply = bbsReplyService.getById(replyId);
            Objs.isTrue(bbsReply != null,"回复不存在");
        }
        return userSupervise.insert();
    }
}

